package com.device.grant.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.device.grant.dao.ChargeLogDao;
import com.device.grant.dao.DepartmentDao;
import com.device.grant.pojo.ChargeLog;
import com.device.grant.pojo.IDepartment;
import com.device.grant.pojo.vo.ChargeLogVo;
import com.device.grant.pojo.vo.IDepartmentVo;
import com.device.grant.pojo.vo.IEmployeeVo;
import com.device.grant.service.ChargeLogService;
import com.device.grant.service.DepartmentService;
import com.device.grant.utils.BeanHelper;
import com.device.grant.utils.ColumnCompator;

@Service("chargeLogService")
public class ChargeLogServiceImpl implements ChargeLogService {

	@Autowired
	private ChargeLogDao		chargeLogDao;

	@Autowired
	private DepartmentDao		departmentDao;

	@Resource(name = "departmentService")
	private DepartmentService	departmentService;

	private static final Logger	logger	= LoggerFactory.getLogger(ChargeLogServiceImpl.class);

	public List<ChargeLogVo> queryChargeLogs(Integer page, Integer rows, Map<String, Object> params,
			List<String> blearParams, Map<String, ColumnCompator> compators, String orderBy) {
		List<ChargeLog> logs = chargeLogDao.queryChargeLogs(page, rows, params, blearParams, compators, orderBy);

		List<ChargeLogVo> logvos = Collections.emptyList();

		if ((null != logs) && !logs.isEmpty()) {
			logvos = new ArrayList<ChargeLogVo>();

			Set<Integer> ids = new HashSet<Integer>();
			for (ChargeLog log : logs) {
				ids.add(log.getDeptID());
			}

			List<IDepartment> ptms = departmentDao.getDepartMentsIncludeParent(ids);

			for (ChargeLog log : logs) {
				ChargeLogVo vo = BeanHelper.getInstanceFromOrigBean(ChargeLogVo.class, log);

				for (IDepartment ptm : ptms) {
					if (ptm.getDeptID().equals(vo.getDeptID())) {
						vo.setDeptName(ptm.getDeptName());
					}
				}

				logvos.add(vo);
			}
		}

		return logvos;
	}

	public Integer countChargeLogs(Integer page, Integer rows, Map<String, Object> params, List<String> blearParams,
			Map<String, ColumnCompator> compators) {
		return chargeLogDao.countChargeLogs(page, rows, params, blearParams, compators);
	}

	public boolean insertLog(ChargeLog log) {
		return chargeLogDao.insertLog(log);
	}

	public boolean doCharge(ChargeLog chargeLog, IEmployeeVo loginer) {
		logger.warn("doCharge - begin. the chargeLog is {}.", chargeLog);

		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("deptID", chargeLog.getDeptID());

		IDepartmentVo vo = departmentService.getTheDepartmentVo(conditions);

		if (chargeLog.getType().equals(1)) {
			vo.setBalance(vo.getBalance() + chargeLog.getCharger());
		} else {
			if (vo.getBalance() < chargeLog.getCharger()) {
				return false;
			}
			vo.setBalance(vo.getBalance() - chargeLog.getCharger());
		}

		IDepartment iDepartment = BeanHelper.getInstanceFromOrigBean(IDepartment.class, vo);

		boolean updateAccount = departmentService.updateDepartMentByBean(iDepartment);

		logger.warn("doCharge - begin. the updateAccount is {}.", updateAccount);

		if (updateAccount) {
			chargeLog.setLoginAccount(loginer.getLoginAccount());
			chargeLog.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			chargeLog.setDeptID(vo.getDeptID());
			chargeLog.setDeptNo(vo.getDeptNo());

			updateAccount = insertLog(chargeLog);
		}

		return updateAccount;
	}
}
